package com.mhl.view;

import com.mhl.domain.DiningTable;
import com.mhl.domain.Employee;
import com.mhl.domain.Menu;
import com.mhl.service.DiningTableService;
import com.mhl.service.EmployeeService;
import com.mhl.service.MenuService;
import com.mhl.utils.Utility;

import java.util.List;

public class MHLView {
    public static void main(String[] args) {
        new MHLView().mainMenu();
    }


    private boolean loop = true;
    private String key = "";
    private EmployeeService employeeService = new EmployeeService();
    private DiningTableService diningTableService = new DiningTableService();
    private MenuService menuService = new MenuService();

    public void listDiningTable(){
        List<DiningTable> list = diningTableService.getDiningTable();
        System.out.println("\n餐桌编号\t\t餐桌状态");
        for(DiningTable diningTable:list){
            System.out.println(diningTable);

        }
        System.out.println("========显示完毕=======");
    }

//    完成订座
    public void orderDiningTable(){
        System.out.println("=====预定餐桌=====");
        System.out.println("请选择需要预定的餐桌编号（-1退出）");
        int orderId = Utility.readInt();
        if (orderId==-1){
            System.out.println("取消预定餐桌");
            return;
        }
//        该方法得到的是Y或者N
        char key = Utility.readConfirmSelection();
        if (key=='Y'){

            DiningTable diningTable = diningTableService.getDiningTableById(orderId);
            if (diningTable==null){
                System.out.println("====预定餐桌不存在====");
                return;
            }
//            判断该餐桌的状态是否”空“
            if (!("空".equals(diningTable.getState()))){
                System.out.println("====该餐桌已经预定或者就餐中====");
                return;
            }
            System.out.println("请输入姓名:");
            String orderName = Utility.readString(50);
            System.out.println("请输入电话:");
            String orderTel = Utility.readString(50);

            if (diningTableService.orderDiningTable(orderId,orderName,orderTel)){
                System.out.println("=====预定成功=====");
            }else{
                System.out.println("=====预定失败=====");
            }
        }else{
            System.out.println("=====取消预订=====");
        }

    }

    public void MenuList(){
        List<Menu> list = menuService.getMenu();
        System.out.println("\n菜品编号\t\t菜品名\t\t类别\t\t价格");
        for(Menu menu:list){
            System.out.println(menu);
        }
        System.out.println("=====显示完毕=====");

    }

    public void mainMenu(){
        System.out.println("\t\t 1 登录满汉楼");
        System.out.println("\t\t 2 退出满汉楼");
        System.out.println("请输入你的选择：");
        key = Utility.readString(1);
        while(loop){
            switch (key){
                case "1":
                    System.out.println("请输入员工号：");
                    String empId = Utility.readString(50);
                    System.out.println("请输入密码：");
                    String pwd = Utility.readString(50);
                    Employee employee = employeeService.SqlSelect(empId,pwd);
                    if (employee!=null){
                        System.out.println("========登陆成功=======");
                        while(loop){
                            System.out.println("=====欢迎来到二级菜单====");
                            System.out.println("\t\t1 显示餐桌状态");
                            System.out.println("\t\t2 预定餐桌");
                            System.out.println("\t\t3 显示所有菜品");
                            System.out.println("\t\t4 点餐服务");
                            System.out.println("\t\t5 查看账单");
                            System.out.println("\t\t6 结账");
                            System.out.println("\t\t9 退出满汉楼");
                            System.out.println("请输入你的选择：");
                            key = Utility.readString(1);
                            switch (key){
                                case "1":
                                    System.out.println("显示餐桌状态");
                                    listDiningTable();
                                    break;
                                case "2":
                                    orderDiningTable();
                                    break;
                                case "3":
                                    System.out.println("显示所有菜品");
                                    MenuList();
                                    break;
                                case "4":
                                    System.out.println("点菜服务");
                                    break;
                                case "5":
                                    System.out.println("查看账单");
                                    break;
                                case "6":
                                    System.out.println("结账");
                                    break;
                                case "9":
                                    System.out.println("退出满汉楼");
                                    loop = false;
                                    break;
                                default:
                                    System.out.println("你的输入有误，请重新输入");
                                    break;
                            }
                        }

                    }else{
                        System.out.println("登陆失败");
                    }

                case "2":
                    loop = false;
                    break;
            }
            System.out.println("你退出了满汉楼");

        }

    }
}
